home *** CD-ROM | disk | FTP | other *** search
/ Young Minds / Young Minds Interactive CD-ROM.ISO / wanderer / save.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-10-26  |  3.6 KB  |  174 lines

  1. #include "wand_head.h"
  2.  
  3. extern char screen[NOOFROWS][ROWLEN+1];
  4. extern int saved_game;
  5.  
  6. struct    saved_game    {
  7.     short    num;
  8.     short    score;
  9.     short    bell;
  10.     short    maxmoves;
  11.     short    num_monsters;
  12. };
  13.  
  14. struct    save_vars    zz;
  15.  
  16. void
  17. save_game(num, score, bell, maxmoves, start_of_list, tail_of_list)
  18. int    num, *score, *bell, maxmoves;
  19. struct    mon_rec    *start_of_list, *tail_of_list;
  20. {
  21.     char    fname[128], *fp;
  22.     FILE    *fo;
  23.     struct    saved_game    s;
  24.     extern    char    *getenv();
  25.     struct    mon_rec    *mp;
  26.  
  27.     clear();
  28.     refresh();
  29.     echo();
  30.     CBOFF;
  31.     endwin();
  32.  
  33.     if ((char *)NULL == (fp = getenv("SAVENAME"))) {
  34.         printf("Saving game.... Filename ? ");
  35.         fflush(stdout);
  36.         fp = fname;
  37.         gets(fp);
  38.     }
  39.     if ((FILE *)NULL == (fo = fopen(fp, W_BIN))) {
  40.         printf("Open error on '%s'n", fp);
  41.         exit(1);
  42.     }
  43.  
  44.     s.num = num;
  45.     s.score = *score;
  46.     s.bell = *bell;
  47.     s.maxmoves = maxmoves;
  48.     s.num_monsters = 0;
  49.  
  50.     mp = start_of_list;        /* first entry is dummy    */
  51.     while (mp != tail_of_list) {
  52.         mp = mp->next;
  53.         s.num_monsters++;    /* count them monsters    */
  54.     }
  55.  
  56.     if ( (1 != fwrite((char *)&s, sizeof(s), 1, fo)) ||
  57.          (1 != fwrite((char *)screen, sizeof(screen), 1, fo)) ||
  58.          (1 != fwrite((char *)&zz, sizeof(zz), 1, fo)) )
  59.     {
  60.         printf("Write error on '%s'n", fname);
  61.         fclose(fo);
  62.         unlink(fname);
  63.         exit(1);
  64.     }
  65.  
  66.     mp = start_of_list;
  67.     while (mp != tail_of_list) {
  68.         /* save them monsters    */
  69.         mp = mp->next;
  70.         if (1 != fwrite((char *)mp, sizeof(struct mon_rec), 1, fo)) {
  71.             printf("Write error on '%s'n", fname);
  72.             fclose(fo);
  73.             unlink(fname);
  74.             exit(1);
  75.         }
  76.     }
  77.  
  78.     fclose(fo);
  79.  
  80.     exit(0);
  81. }
  82.  
  83. void
  84. restore_game(num, score, bell, maxmoves, start_of_list, tail_of_list)
  85. int    *num, *score, *bell, *maxmoves;
  86. struct    mon_rec    *start_of_list, **tail_of_list;
  87. {
  88.     FILE    *fi;
  89.     struct    saved_game    s;
  90.     struct    mon_rec    *mp, *tmp, tmp_monst;
  91.     char    fname[128], *fp;
  92.     FILE    *fo;
  93.     extern    char    *getenv();
  94.  
  95.     if ((char *)NULL == (fp = getenv("SAVENAME"))) {
  96.         move((LINES-1),0);
  97.         addstr("Restore Filename ? ");
  98.         refresh();
  99.         echo(); CBOFF;
  100.         fp = fname;
  101.         gets(fp);
  102.         CBON; noecho();
  103.     }
  104.     clear();
  105.     refresh();
  106.  
  107.     if ((FILE *)NULL == (fi = fopen(fp, R_BIN))) {
  108.         endwin();
  109.         printf("Open error on '%s'n", fp);
  110.         exit(1);
  111.     }
  112.     if ( (1 != fread((char *)&s, sizeof(s), 1, fi)) ||
  113.          (1 != fread((char *)screen, sizeof(screen), 1, fi)) ||
  114.          (1 != fread((char *)&zz, sizeof(zz), 1, fi)) ) {
  115.         endwin();
  116.         printf("Read error on '%s'n", fp);
  117.         fclose(fi);
  118.         exit(1);
  119.     }
  120.  
  121.     *num = s.num;
  122.     *score = s.score;
  123.     *bell = s.bell;
  124.     *maxmoves = s.maxmoves;
  125.  
  126.     /* free any monsters already on chain, to start clean */
  127.     mp = start_of_list->next;
  128.     while ((mp != NULL) && (mp != start_of_list)) {
  129.         /* free them monsters    */
  130.         tmp = mp;
  131.         mp = mp->next;
  132.         free(tmp);
  133.     }
  134.  
  135.     /* re-initialize the monster list    */
  136.     /* *start_of_list = {0,0,0,0,0,NULL,NULL}; */
  137.     start_of_list->x = 0;
  138.     start_of_list->y = 0;
  139.     start_of_list->mx = 0;
  140.     start_of_list->my = 0;
  141.     start_of_list->under = 0;
  142.     start_of_list->next = (struct mon_rec *)NULL;
  143.     start_of_list->prev = (struct mon_rec *)NULL;
  144.  
  145.     *tail_of_list = start_of_list;
  146.  
  147.     while (s.num_monsters--) {
  148.         /* use make_monster to allocate the monster structures    */
  149.         /* to get all the linking right without even trying    */
  150.         if ((struct mon_rec *)NULL == (mp = make_monster(0, 0))) {
  151.             endwin();
  152.             printf("Monster alloc error on '%s'n", fp);
  153.             fclose(fi);
  154.             exit(1);
  155.         }
  156.         if (1 != fread((char *)&tmp_monst, sizeof(struct mon_rec), 1, fi)) {
  157.             endwin();
  158.             printf("Monster read error on '%s'n", fp);
  159.             fclose(fi);
  160.             exit(1);
  161.         }
  162.         /* copy info without trashing links    */
  163.         mp->x     = tmp_monst.x;
  164.         mp->y     = tmp_monst.y;
  165.         mp->mx    = tmp_monst.mx;
  166.         mp->my    = tmp_monst.my;
  167.         mp->under = tmp_monst.under;
  168.     }
  169.  
  170.     fclose(fi);
  171.     unlink(fp);
  172.     saved_game = 1;
  173. }
  174.